//
//  BKGDEvaluationTopView.m
//  BESTKEEP
//
//  Created by 武探 on 2016/10/10.
//  Copyright © 2016年 YISHANG. All rights reserved.
//

#import "BKGDEvaluationTopView.h"

@implementation BKGDProgressView {
    CAShapeLayer *_progressLayer;
    CAShapeLayer *_trackLayer;
}

-(instancetype)initWithFrame:(CGRect)frame {
    if(CGSizeEqualToSize(frame.size, CGSizeZero)) {
        frame.size = CGSizeMake(100, 6);
    }
    if(frame.size.height < 6) {
        frame.size.height = 6;
    }
    
    self = [super initWithFrame:frame];
    if(self) {
        [self initialize];
    }
    
    return self;
}

-(void)initialize {
    _progressTintColor = [UIColor colorWithRed:218/255.0 green:218/255.0 blue:218/255.0 alpha:1.00];
    _trackTintColor = [UIColor colorWithRed:0 green:186/255.0 blue:156/255.0 alpha:1.00];
    _progress = 0.0;
    self.backgroundColor = [UIColor clearColor];
    
    _progressLayer = [[CAShapeLayer alloc] init];
    _progressLayer.frame = self.bounds;
    _progressLayer.fillColor = [[UIColor clearColor] CGColor];
    _progressLayer.strokeColor = _progressTintColor.CGColor;
    _progressLayer.lineCap = kCALineCapRound;
    _progressLayer.lineWidth = CGRectGetHeight(self.bounds);
    _progressLayer.strokeEnd = 1;
    [self.layer addSublayer:_progressLayer];
    
    _trackLayer = [[CAShapeLayer alloc] init];
    _trackLayer.frame = self.bounds;
    _trackLayer.fillColor = [[UIColor clearColor] CGColor];
    _trackLayer.strokeColor = _trackTintColor.CGColor;
    _trackLayer.lineCap = kCALineCapRound;
    _trackLayer.lineWidth = CGRectGetHeight(self.bounds);
    _trackLayer.strokeEnd = 0;
    [self.layer addSublayer:_trackLayer];
}

-(void)layoutSubviews {
    [super layoutSubviews];
    
    _progressLayer.frame = self.bounds;
    _trackLayer.frame = self.bounds;
    
    [self updatePath];
}

-(void)updatePath {
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(0, CGRectGetMidY(self.bounds))];
    [path addLineToPoint:CGPointMake(CGRectGetMaxX(self.bounds), CGRectGetMidY(self.bounds))];
    
    _progressLayer.path = path.CGPath;
    
    _trackLayer.path = path.CGPath;
}

-(void)setProgress:(CGFloat)progress animated:(BOOL)animated {
    progress = progress < 0 ? 0 : progress > 1 ? 1 : progress;
    
    CGFloat currentProgress = _progress;
    
    _progress = progress;
    
    if(animated) {
        CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
        anim.duration = 0.5;
        anim.fromValue = @(currentProgress);
        anim.toValue = @(_progress);
        anim.fillMode = kCAFillModeForwards;
        anim.removedOnCompletion = NO;
        [_trackLayer addAnimation:anim forKey:@"progress"];
    } else {
        _trackLayer.strokeEnd = _progress;
    }
}

-(void)setProgress:(CGFloat)progress {
    progress = progress < 0 ? 0 : progress > 1 ? 1 : progress;
    _progress = progress;
    _trackLayer.strokeEnd = _progress;
}

@end







@interface BKGDEvaluationTopView()

/**
 好评ProgressView
 */
@property(nonatomic,strong) BKGDProgressView *haopingProgressView;
/**
 中评
 */
@property(nonatomic,strong) BKGDProgressView *zhongpingProgressView;
/**
 差评
 */
@property(nonatomic,strong) BKGDProgressView *chapingProgressView;

/**
 满意度
 */
@property(nonatomic,strong) UILabel *satisfactionLabel;
@property(nonatomic,strong) UILabel *haopingLabel;
@property(nonatomic,strong) UILabel *zhongpingLabel;
@property(nonatomic,strong) UILabel *chapingLabel;

@end

@implementation BKGDEvaluationTopView

-(instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame: frame];
    if(self) {
        [self initialize];
    }
    return self;
}

-(void)initialize {
    
    self.backgroundColor = [UIColor whiteColor];
    
    _satisfactionLabel = [[UILabel alloc] init];
    _satisfactionLabel.numberOfLines = 0;
    _satisfactionLabel.lineBreakMode = NSLineBreakByWordWrapping;
    _satisfactionLabel.textAlignment = NSTextAlignmentCenter;
    _satisfactionLabel.font = [UIFont systemFontOfSize:18];
    _satisfactionLabel.textColor = [UIColor colorWithRed:218/255.0 green:218/255.0 blue:218/255.0 alpha:1.00];
    [self addSubview:_satisfactionLabel];
    
    _haopingLabel = [self createPinglunLabel];
    _haopingLabel.text = BKGDLocalizedString(@"BKGDEvaluationTopGood-review");
    [self addSubview:_haopingLabel];
    
    _zhongpingLabel = [self createPinglunLabel];
    _zhongpingLabel.text = BKGDLocalizedString(@"BKGDEvaluationTopMedium-review");
    [self addSubview:_zhongpingLabel];
    
    _chapingLabel = [self createPinglunLabel];
    _chapingLabel.text = BKGDLocalizedString(@"BKGDEvaluationTopBad-review");
    [self addSubview:_chapingLabel];
    
    _haopingProgressView = [[BKGDProgressView alloc] init];
    [_haopingProgressView setProgress:1];
    [self addSubview:_haopingProgressView];
    
    _zhongpingProgressView = [[BKGDProgressView alloc] init];
    [self addSubview:_zhongpingProgressView];
    
    _chapingProgressView = [[BKGDProgressView alloc] init];
    [self addSubview:_chapingProgressView];
    
    [self makeConstraints];
    
    _satisfactionLabel.attributedText = [self satisfactionAttributedStringWithString:@"100"];
}

-(void)makeConstraints {
    [_satisfactionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.left.equalTo(self);
        make.bottom.equalTo(self);
        make.width.mas_equalTo(CGRectGetWidth(self.frame) * 0.4);
    }];
    
    [_haopingLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(15);
        make.left.equalTo(_satisfactionLabel.mas_right);
    }];
    
    [_haopingProgressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_haopingLabel);
        make.left.equalTo(_haopingLabel.mas_right).offset(15);
        make.right.mas_equalTo(-15);
    }];

    [_zhongpingLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_haopingLabel.mas_bottom).offset(15);
        make.left.equalTo(_haopingLabel);
    }];
    
    [_zhongpingProgressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_zhongpingLabel);
        make.left.equalTo(_haopingProgressView);
        make.right.equalTo(_haopingProgressView);
    }];
    
    [_chapingLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_zhongpingLabel.mas_bottom).offset(15);
        make.left.equalTo(_zhongpingLabel);
    }];
    
    [_chapingProgressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_chapingLabel);
        make.left.equalTo(_zhongpingProgressView);
        make.right.equalTo(_zhongpingProgressView);
    }];
}

-(UILabel*)createPinglunLabel {
    UILabel *label = [[UILabel alloc] init];
    label.font = [UIFont systemFontOfSize:14];
    label.textColor = [UIColor colorWithRed:136/255.0 green:136/255.0 blue:136/255.0 alpha:1.00];
    return label;
}

-(void)setEvaluationTop:(BKGDEvaluationTopObject *)evaluationTop {
    
    if(!evaluationTop) {
        return;
    }
    
    _evaluationTop = evaluationTop;
    
    _satisfactionLabel.attributedText = [self satisfactionAttributedStringWithString:evaluationTop.satisfactionPct];
    [_haopingProgressView setProgress:[self progressWithNumberString:evaluationTop.haopingPct]];
    [_zhongpingProgressView setProgress:[self progressWithNumberString:evaluationTop.zhongpingPct]];
    [_chapingProgressView setProgress:[self progressWithNumberString:evaluationTop.chapingPct]];
}

-(NSAttributedString*)satisfactionAttributedStringWithString:(NSString*)satString {
    NSMutableAttributedString *satAttString = [[NSMutableAttributedString alloc] init];
    [satAttString appendAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@%%", satString] attributes:@{NSFontAttributeName: [UIFont boldSystemFontOfSize:30], NSForegroundColorAttributeName: [UIColor colorWithRed:0 green:186/255.0 blue:156/255.0 alpha:1.00]}]];
    [satAttString appendAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"\n%@", BKGDLocalizedString(@"BKGDEvaluationTopTitle")] attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:14], NSForegroundColorAttributeName: _haopingLabel.textColor}]];
    
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    style.lineSpacing = 10;
    style.alignment = NSTextAlignmentCenter;
    [satAttString addAttributes:@{NSParagraphStyleAttributeName: style} range:(NSRange){0, satAttString.string.length}];
    
    return [[NSAttributedString alloc] initWithAttributedString:satAttString];
}

-(CGFloat)progressWithNumberString:(NSString*)str {
    CGFloat v = [str floatValue];
    
    CGFloat progress = v / 100.0;
    
    return progress;
}

@end
